package org.gruposp2p.proyectokarma.gwt.client.panel;

import java.util.Set;
import org.gruposp2p.proyectokarma.gwt.client.domain.Tag;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.FontStyle;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.allen_sauer.gwt.log.client.Log;
import org.gruposp2p.proyectokarma.gwt.client.event.EventBusManager;
import org.gruposp2p.proyectokarma.gwt.client.event.TagEvent;
import org.gruposp2p.proyectokarma.gwt.client.event.TagsEvent;
import org.gruposp2p.proyectokarma.gwt.client.event.TagEvent.Action;
import org.gruposp2p.proyectokarma.gwt.client.ui.TopPanel;

public class VerticalTagsPanel extends Composite implements TagsEvent.Handler {

    interface VerticalTagsPanelUiBinder extends UiBinder<Widget, VerticalTagsPanel> { }
    private static VerticalTagsPanelUiBinder uiBinder = GWT.create(VerticalTagsPanelUiBinder.class);

    @UiField VerticalPanel mainPanel;
    Set<String> tagSet;

    public VerticalTagsPanel() {
    	initWidget(uiBinder.createAndBindUi(this));
        EventBusManager.getInstance().registerToTagsEvent(this);
    }
    
    public void loadMostCommonTags (Set<Tag> tagSet) {
    	TagClickHandler tagClickHandler = 
    		new TagClickHandler();
    	HTML tagsPanelTitle = new HTML ("Etiquetas:");
    	tagsPanelTitle.getElement().getStyle().setFontWeight(FontWeight.BOLD);
    	tagsPanelTitle.getElement().getStyle().setFontStyle(FontStyle.OBLIQUE);
    	mainPanel.add(tagsPanelTitle); 
    	for (Tag tag :  tagSet) {
    		HorizontalPanel tagPanel = new HorizontalPanel();
        	Anchor tagAnchor = new Anchor(tag.getName());
        	tagAnchor.addClickHandler(tagClickHandler);
        	tagAnchor.setStylePrimaryName("clickableLabel"); 
        	tagPanel.add(tagAnchor);
        	HTML frequency = new HTML(" x " + tag.getFrequency());
        	frequency.getElement().getStyle().setMarginTop(8, Unit.PX);
        	frequency.getElement().getStyle().setFontWeight(FontWeight.BOLD);
        	tagPanel.add(frequency);
        	mainPanel.add(tagPanel); 
    	}
    }
	
    private class TagClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			String selectedTag = ((Anchor)event.getSource()).getText();
			TagEvent tagEvent = new TagEvent(Action.GET_DOCUMENTS, selectedTag);
			EventBusManager.getInstance().fireEvent(tagEvent);
        	History.newItem(TopPanel.HISTORY_TAG);
        	History.fireCurrentHistoryState(); 
		}	
	}
    
    public interface Handler {
    	void handleSelectedTag(String selectedTag);
    }


	@Override
	public void onTagsChanged(TagsEvent event) {
		switch (event.eventAction) {
			case OBTAINED:
    			loadMostCommonTags(event.tags);
			break;
		}
		
	}
    
}